import http from '../../apis/api.js';
import config from '../../config.js';
import util from '../../utils/util.js';
const app = getApp();
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    latitude: '',
    longitude: '',
  },
  onLoad: function (options) {
    self=this;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  getOpenid() {
    self.setData({
      loading: true
    })
    wx.login({
      success(res) {
        if (res.code) {
          http.getOpenId({ code: res.code }).then(result => {
            wx.setStorage({ key: 'token', data: result.content });
            let url = '/pages/index/index';
            if (result.statusCode === '300') {
              url = "/pages/roleRegist/roleRegist"
            }
            wx.redirectTo({
              url,
            })
          }, error => {
            self.setData({
              loading: false
            })
          })
        }
      },
      fail() {
        wx.showToast({
          title: '获取登录凭证失败',
          icon: 'none',
          duration: 2000
        })
        self.setData({
          loading: false
        })
      }
    })
   
  },
  onGotUserInfo(e){
    if (e.detail.errMsg ==='getUserInfo:ok'){
      wx.setStorage({
        key: 'userInfo',
        data: e.detail.userInfo,
        success(e) {
          self.getOpenid();
        }
      });
    }else{
      self.getOpenid();
    }
  }
})